package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.cloudcc.cloudframe.adapter.CommonAdapter;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.CoworkerEntity;
import com.cloudcc.mobile.im.UIControl;
import com.cloudcc.mobile.util.SystemUtils;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.UserUtils;

/**
 * 同事适配器
 * 
 * @author Administrator
 * 
 */
public class CoworkerListAdapter extends CommonAdapter<CoworkerEntity> {

	protected final int[] colors;
	private OnItemFnClickListener mOnItemFnClickListener;
	public CoworkerListAdapter(Context mContext) {
		super(mContext);
		String[] array = mContext.getResources().getStringArray(R.array.logo_colors);
		colors =new int[array.length];
		for (int i = 0; i < array.length; i++) {
			colors[i] =Color.parseColor(array[i]);
		}
	}

	@Override
	public void convert(com.cloudcc.cloudframe.adapter.ViewHolder holder,
			CoworkerEntity t) {
		ImageView ivLogo = holder.getView(R.id.contact_item_name);
		String uri = UrlTools.getTopImage(t.getId());
		int colorindex=holder.getPostion()%colors.length;
		UserUtils.setLogoRound(uri, ivLogo, colors[colorindex], t.getNickRowName(), mContext);
		holder.setText(R.id.contact_item_logo, t.getName());

		// 功能
		ImageView btnAtt = holder.getView(R.id.contact_item_att);//关注
		ImageView btnMobile = holder.getView(R.id.contact_item_phone);//电话
		ImageView btnSms = holder.getView(R.id.contact_item_email);//短信
		ImageView btnChat = holder.getView(R.id.contact_item_chat);//IM
		
		final CoworkerEntity finalt =t;
		final int pos =holder.getPostion();
		//关注
		btnAtt.setImageResource(t.isIsmefollow()?R.drawable.chat_contaxt_zhuisuit:R.drawable.chat_contaxt_zhuisuif);
		btnAtt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mOnItemFnClickListener!=null) {
					mOnItemFnClickListener.onClickAtt(pos, finalt);
				}
			}
		});
		//电话
		boolean notBlank = StringUtils.isNotBlank(t.getMobile());
		btnMobile.setVisibility(notBlank?View.VISIBLE:View.INVISIBLE);
		btnSms.setVisibility(notBlank?View.VISIBLE:View.INVISIBLE);
		btnMobile.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SystemUtils.sendCall(mContext, finalt.getMobile());
			}
		});
		btnSms.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				SystemUtils.sendSms(mContext, finalt.getMobile());
			}
		});
		btnChat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserUtils.isMe(finalt.getId())) {
					Toast.makeText(mContext, "对不起不能自己跟自己聊天...",0).show();
				}else{
					UIControl.openChatUI(mContext, finalt.getHxusername(), finalt.getName(), false);
				}
			}
		});
	}

	@Override
	public int getLayoutId() {
		return R.layout.tongshi_list_item;
	}

	public interface OnItemFnClickListener{
		/** 点击关注的时候
		 * @param pos
		 * @param t
		 */
		void onClickAtt(int pos,CoworkerEntity t);
	}
	
	/** item功能相点击的时候
	 * @param l
	 */
	public void setOnItemFnClickListener(OnItemFnClickListener l){
		this.mOnItemFnClickListener =l;
	}

}